<?php
namespace app\api\library;


use app\common\model\Orders;

class OrderSn {

    public static function create()
    {
        do
        {
            $time = explode(' ', microtime());
            $time = $time[1] . $time[0] * 1000;
            $time = explode('.', $time);
            $time = isset($time[1]) ? $time[1] : 0;
            $time = date('YmdHis') + $time;
            mt_srand((double) microtime() * 1000000);
            $order_sn = $time . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            $res = Orders::getByOrderSn($order_sn);
        }
        while ($res);
        return $order_sn;
    }

    public static function create2()
    {
        $time = explode(' ', microtime());
        $time = $time[1] . $time[0] * 1000;
        $time = explode('.', $time);
        $time = isset($time[1]) ? $time[1] : 0;
        $time = date('YmdHis') + $time;
        mt_srand((double) microtime() * 1000000);
        $order_sn = $time . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        return $order_sn;
    }

    public static function createByTime($time = null)
    {
        do
        {
            $order_sn = date('YmdHis',$time).rand(10,99);
            $res = Orders::getByOrderSn($order_sn);
        }
        while ($res);
        return $order_sn;
    }
}